package myweb

import (
	"fmt"
	"io/ioutil"
	"main/myInterface"
	"net/http"

	"github.com/gin-gonic/gin"
)

func echatsData(r *gin.Engine, ws string) {
	r.GET("/chart", func(c *gin.Context) {
		myInterface.SerialFlag = true
		c.HTML(http.StatusOK, "chart.html", gin.H{
			"ws": ws,
		})
	})

	r.POST("/chartsend", func(c *gin.Context) {
		b, err := ioutil.ReadAll(c.Request.Body)
		if err != nil {
			fmt.Print(err)
		}
		fmt.Println(b)
		myInterface.SerialTime = 0
		myInterface.SerialStr = ""
		myInterface.SerialWrite([]byte{1})

		for {
			if myInterface.SerialTime > 10000 {
				fmt.Print(myInterface.SerialStr)
				break
			}
		}
		c.String(http.StatusOK, myInterface.SerialStr)
	})

	r.POST("/restartPost", func(c *gin.Context) {
		b, err := ioutil.ReadAll(c.Request.Body)
		if err != nil {
			fmt.Print(err)
		}
		fmt.Println(b)
		myInterface.SerialWrite([]byte{3})

		c.String(http.StatusOK, "ok")
	})
}
